package insertData;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

public class BatchInsertRandomData {
    public static void main(String[] args) {
        try {
            Connection connection = DatabaseConnection.getConnection();

            // Replace these values with the appropriate start and end years
            int startYear = 2020;
            int endYear = 2023;

            Random random = new Random();
            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Calendar calendar = Calendar.getInstance();
            calendar.set(startYear, Calendar.JANUARY, 1);

            for (int year = startYear; year <= endYear; year++) {
                String tableName = "uf_t0_" + year;
                List<String> modecreatedates = new ArrayList<>();
                List<String> wbs = new ArrayList<>();

                int totalRecords = 14000000;
                int recordsPerMonth = totalRecords / 12;

                for (int month = Calendar.JANUARY; month <= Calendar.DECEMBER; month++) {
                    calendar.set(year, month, 1);
                    int monthRecords = recordsPerMonth + random.nextInt(recordsPerMonth); // Vary the records
                    for (int i = 0; i < monthRecords; i++) {
                        Date modecreatedate = calendar.getTime();
                        String wb = generateRandomString(random, 5);
                        modecreatedates.add(dateFormat.format(modecreatedate));
                        wbs.add(wb);
                    }
                }
                insertBatchDataIntoTable(connection, tableName, modecreatedates, wbs);
                System.out.println("Inserted data into " + tableName);
            }

            connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static String generateRandomDate(int year, Random random, DateFormat dateFormat) {
        int month = random.nextInt(12) + 1;
        int day = random.nextInt(28) + 1; // Assume all months have max 28 days for simplicity
        return year + "-" + String.format("%02d", month) + "-" + String.format("%02d", day);
    }

    public static String generateRandomString(Random random, int length) {
        String characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < length; i++) {
            int index = random.nextInt(characters.length());
            sb.append(characters.charAt(index));
        }
        return sb.toString();
    }

    public static void insertBatchDataIntoTable(Connection connection, String tableName, List<String> modecreatedates, List<String> wbs) throws SQLException {
        String insertQuery = "INSERT INTO " + tableName + " (modecreatedate, wb) VALUES (?, ?)";
        try (PreparedStatement preparedStatement = connection.prepareStatement(insertQuery)) {
            for (int i = 0; i < modecreatedates.size(); i++) {
                preparedStatement.setString(1, modecreatedates.get(i));
                preparedStatement.setString(2, wbs.get(i));
                preparedStatement.addBatch();
            }
            preparedStatement.executeBatch();
        }
    }
}
